package com.sjm.suchbear.minibookmall.fragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.fragment.CarFragment;
import com.sjm.suchbear.minibookmall.model.CarModel;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by GST on 2018/5/7.
 */

public class CarListAdapter extends BaseAdapter {

    private ViewHolder viewHolder;
    private Context context;
    private List<CarModel> cars = new ArrayList<>();
    private boolean showCheck = false;

    public CarListAdapter(Context context, List<CarModel> cars) {
        this.context = context;
        this.cars = cars;
    }


    @Override
    public int getCount() {
        if (cars != null) {
            return cars.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return cars.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_show, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CarModel carModel = cars.get(i);
        viewHolder.tvName.setText(carModel.getBookname());
        viewHolder.tvSec.setText("数量：".concat(carModel.getNum() + "本"));
        BigDecimal cost = new BigDecimal(carModel.getPrice());
        BigDecimal num = new BigDecimal(carModel.getNum());
        DecimalFormat myformat = new java.text.DecimalFormat("0.00");
        viewHolder.tvPrice.setText("¥".concat(myformat.format(cost.multiply(num))));
        if (showCheck) {
            viewHolder.rlChoose.setVisibility(View.VISIBLE);
            viewHolder.cbChoose.setChecked(carModel.isChecked());
            viewHolder.cbChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (showCheck) {
                        CarFragment.setChecked(i, b);
                    }
                }
            });
        } else {
            viewHolder.rlChoose.setVisibility(View.GONE);
        }
        return convertView;
    }

    public void isCheckedMode(boolean check) {
        showCheck = check;
    }

    public boolean getisCheckMode() {
        return showCheck;
    }

    static class ViewHolder {
        @BindView(R.id.cb_choose)
        CheckBox cbChoose;
        @BindView(R.id.rl_choose)
        RelativeLayout rlChoose;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_sec)
        TextView tvSec;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
